<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">大白影视系统</h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
                  @keyup.enter="handleLogin">
        </el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>

import {getCurrentInstance, ref} from "vue";
import router from "@/router/index.js";
import {ElMessage} from "element-plus";
import {getUserList} from "@/api/user.js";
import store from "@/store/index.js";

const isAdmin = ref(false)
const currentUser = ref({
  uid:'',
  username:'',
  password:'',
  nickname:'',
  phone:'',
  isAdmin: 0
})
const userData = ref([{
  username:'',
  password: ''
}])
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: '',
  password: '',
});
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
};
const loading = ref(false);
function handleLogin() {
  proxy.$refs.loginRef.validate(async valid => {
    if (valid) {
      loading.value = true;
      const result = await check();
      console.log("isAdmin", isAdmin.value);
      if (isAdmin.value) {
        ElMessage.success("登录成功,欢迎管理员");
        entryAdmin();
      } else {
        ElMessage.success("登录成功,欢迎用户");
        entryUser();
      }
      loading.value = false;
    }
  });
}
function entryAdmin(){
  router.push({
    path:'/main',
  })

}
function entryUser(){
  router.push({
    path:'/'
  })
}
async function check() {
  const res = await getUserList();
  userData.value = res.data.data;
  for (const user of userData.value) {
    if (user.username === loginForm.value.username && user.password === loginForm.value.password) {
      isAdmin.value = user.isAdmin === 1;
      await store.dispatch('asyncUpdateUser', {name: user.uid})
      console.log(currentUser.value)
      return true;
    }
  }
  return false;
}
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0;
  }
}
</style>
